<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\AdminUser\Action\Admin;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\AdminUser\Contract\AdminUserContract;
use MoChat\Plugin\AdminUser\Middleware\AdminAuthMiddleware;
use MoChat\Plugin\AdminUser\Middleware\PermissionMiddleware;

/**
 * 账户管理- 修改.
 *
 * Class Update.
 * @Controller
 */
class Update extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var AdminUserContract
     */
    protected $adminUserService;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    /**
     * @RequestMapping(path="/admin/adminUser/update", methods="PUT")
     * @Middlewares({
     *     @Middleware(AdminAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 参数验证
        $this->validated($this->request->all());
        $adminUser = user();
        // 接收参数
        $id = $this->request->input('id');
        $params = [
            'name' => $this->request->input('name'),
            'company_name' => $this->request->input('companyName'),
            'company_image' => $this->request->input('companyImage'),
            'area' => $this->request->input('area'),
            'description' => $this->request->input('description'),
            'level' => $this->request->input('level'),
            'wallet_balance' => $this->request->input('walletBalance', 0),
        ];

        // 数据操作
        Db::beginTransaction();
        try {
            $this->adminUserService->updateAdminUserById($id, $params);
            Db::commit();
        } catch (\Throwable $e) {
            Db::rollBack();
            $this->logger->error(sprintf('%s [%s] %s', '账户修改失败', date('Y-m-d H:i:s'), $e->getMessage()));
            $this->logger->error($e->getTraceAsString());
            throw new CommonException(ErrorCode::SERVER_ERROR, '账户修改失败');
        }
        return [];
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'name' => 'required | string | min:1 | bail',
            'area' => 'required | string | min:1 | bail',
            'level' => 'required | integer | bail',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'name.required' => '用户名称 必填',
            'name.string' => '用户名称 必需为字符串',
            'name.min' => '用户名称 字符串长度不可小于1',
            'area.required' => '所属地区 必填',
            'area.string' => '所属地区 必需为字符串',
            'area.min' => '所属地区 字符串长度不可小于1',
            'level.required' => '代理等级 必填',
            'level.integer' => '代理等级 必需为整数',
        ];
    }
}
